App({
  onLaunch: function () {

    var that     = this;
    var user     = wx.getStorageSync('user') || {};     //存储用户的openid token 
    var userInfo = wx.getStorageSync('userInfo') || {}; //存储用户的信息

    //可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              wx.getUserInfo()
            }
          })
        }
      }
    })

     console.log(userInfo);return;
    wx.showToast({
      title: '成功',
      icon: 'success',
      duration: 2000
    });
    wx.showLoading({
      title: '加载中',
    });
    if((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600))&&(!userInfo.nickName)){ 
      wx.login({  
        success: function(res){ 
            // console.log(res);
            if(res.code) {
              wx.getUserInfo({
                  success: function(res) {
                      var userData        = {};
                      userData.avatarUrl  = res.userInfo.avatarUrl;
                      userData.nickName   = res.userInfo.nickName;
                      console.log(userData);
                      wx.setStorageSync('userInfo', userData); 
                  }
                  // fail: function() {
                  //   console.log(12312);
                  // }
              });
              var own   = that.globalData;//这里存储了appid secret token串  
              var path  = 'https://api.weixin.qq.com/sns/jscode2session?appid='+own.appid+'&secret='+own.secret+'&js_code='+res.code+'&grant_type=authorization_code';  
              wx.request({  
                  url: path,  
                  // data: {},  
                  method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
                  header: {
                    "Content-Type":"application/json"
                  }, // 设置请求的 header  
                  success: function(res){ 
                      console.log(res);
                      var userinfo          = {};
                      userinfo.openid       = res.data.openid;  
                      userinfo.expires_in   = Date.now() + res.data.expires_in;  
                      // console.log(userinfo);
                      wx.setStorageSync('user', userinfo);  
                  }  
              });
            }else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }          
        }  
      }); 
    } 
  },
  globalData: {
    appid: 'wx7a4f409c7ab6cea5',               //appid需自己提供
    secret: '928470666a862fcbc41e8c151d115da1', //secret需自己提供
    url: 'http://www.xyun.com/api/'          //请求数据地址

  },
})
